package org.tvolkov.mycompany

class AppController {
	
	static final def ROOT_TOKEN = "root";
	static final def COMPANY_TOKEN = "company";
	static final def DIVISION_TOKEN = "division";
	static final def PROJECT_TOKEN = "project";
	static final def EMPLOYEE_TOKEN = "employee";
	
	def get = {
		def node = params["node"];
		println("AppController: get: params: " + params);
		if (node == ROOT_TOKEN){
			redirect (controller: "company", action: "list")
		} else if (node =~ COMPANY_TOKEN){
			redirect (controller: "division", action: "list", params: [id: calculateNodeId(params["id"])])
		} else if (node =~ DIVISION_TOKEN){
			redirect (controller: "project", action: "list", params: [id: calculateNodeId(params["id"])])
		} else if (node =~ PROJECT_TOKEN){
			redirect (controller: "employee", action: "list", params: [id: calculateNodeId(params["id"])])
		}
	}

	def add = {
		println("AppController: add: params: " + params);
		def pnode = params["parentNode"];
		if (pnode == ROOT_TOKEN){
			redirect (controller: "company", action: "add", params: params);
		} else if (pnode =~ COMPANY_TOKEN){
			params.put("parentCompany", calculateNodeId(pnode));
			redirect (controller: "division", action: "add", params: params);
		} else if (pnode =~ DIVISION_TOKEN){
			params.put("parentDivision", calculateNodeId(pnode));
			redirect (controller: "project", action: "add", params: params);
		} else if (pnode =~ PROJECT_TOKEN){
			params.put("parentProject", calculateNodeId(pnode));
			redirect (controller: "employee", action: "add", params: params);
		}
	}
	
	def remove = {
		println("AppController: remove: " + params);
		def node = params["node"];
		def nodeId = calculateNodeId(node);
		if (node =~ COMPANY_TOKEN){
			redirect(controller: "company", action: "remove", params: [nodeid: nodeId]);
		} else if (node =~ DIVISION_TOKEN){
			redirect(controller: "division", action: "remove", params: [nodeid: nodeId]);
		} else if (node =~ PROJECT_TOKEN){
			redirect(controller: "project", action: "remove", params: [nodeid: nodeId]);
		} else if (node =~ EMPLOYEE_TOKEN){
			redirect(controller: "employee", action: "remove", params: [nodeid: nodeId]);
		}
	}
	
	def details = {
		println(params);
		def node = params["nodeid"]
		def nodeId = calculateNodeId(node);
		
		if (node =~ COMPANY_TOKEN){
			redirect(controller: "company", action: "details", params: [nodeid: nodeId]);	
		} else if (node =~ DIVISION_TOKEN){
			redirect(controller: "division", action: "details", params: [nodeid: nodeId]);
		} else if (node =~ PROJECT_TOKEN){
			redirect(controller: "project", action: "details", params: [nodeid: nodeId]);
		} else if (node =~ EMPLOYEE_TOKEN){
			redirect(controller: "employee", action: "details", params: [nodeid: nodeId]);
		}
	}
	
	def edit = {
		println("AppController: edit: params: " + params);
		def node = params["nodeid"];
		def nodeid = calculateNodeId(node);
		println("AppController: edit: node: " + node);
		println("AppController: edit: nodeid: " + nodeid);
		if (node =~ COMPANY_TOKEN){
			params["nodeid"] = nodeid;
			redirect(controller: "company", action: "edit", params: params);	
		} else if (node =~ DIVISION_TOKEN){
			params["nodeid"] = nodeid;
			redirect(controller: "division", action: "edit", params: params);
		} else if (node =~ PROJECT_TOKEN){
			params["nodeid"] = nodeid;
			redirect(controller: "project", action: "edit", params: params);
		} else if (node =~ EMPLOYEE_TOKEN){
			params["nodeid"] = nodeid;
			redirect(controller: "employee", action: "edit", params: params);
		}
	}
	def calculateNodeId(iD){
		if (iD =~ COMPANY_TOKEN){
			return iD.minus(COMPANY_TOKEN).toLong();
		} else if (iD =~ DIVISION_TOKEN) {
			return iD.minus(DIVISION_TOKEN).toLong();
		} else if (iD =~ PROJECT_TOKEN) {
			return iD.minus(PROJECT_TOKEN).toLong();
		} else if (iD =~ EMPLOYEE_TOKEN){
			return iD.minus(EMPLOYEE_TOKEN).toLong();
		}
	}
}
